


// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class SeatOfPlanes
	{

		#region Constructors

	    public SeatOfPlanes()
		{}
	    
	
	    public SeatOfPlanes(
			long planeId, 
			int seatClassId) 
		{
	        GetSeatOfPlanes(
				planeId, 
				seatClassId); 
	    }

		#endregion

		#region Private Properties

		private long id;
		private long planeId;
		private int seatClassId = -1;
		private int totalSeat = -1;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;
		
		#endregion

		#region Public Properties

		public long Id 
		{
			get { return id; }
			set { id = value; }
		}
		public long PlaneId 
		{
			get { return planeId; }
			set { planeId = value; }
		}
		public int SeatClassId 
		{
			get { return seatClassId; }
			set { seatClassId = value; }
		}
		public int TotalSeat 
		{
			get { return totalSeat; }
			set { totalSeat = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of SeatOfPlanes.
		/// </summary>
		/// <param name="planeId"> planeId </param>
		/// <param name="seatClassId"> seatClassId </param>
		private void GetSeatOfPlanes(
			long planeId, 
			int seatClassId) 
		{
			try
			{
				using(IDataReader reader = DBSeatOfPlanes.GetOne(
					planeId, 
					seatClassId)) 
				{
					PopulateFromReader(reader);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt64(reader["Id"]);
					this.planeId = Convert.ToInt64(reader["PlaneId"]);
					this.seatClassId = Convert.ToInt32(reader["SeatClassId"]);
					this.totalSeat = Convert.ToInt32(reader["TotalSeat"]);
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of SeatOfPlanes. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBSeatOfPlanes.Create(
					this.planeId,
                    this.seatClassId,
					this.totalSeat, 
					this.createdDate, 
					this.lastUpdate); 
					
				this.id = newID;
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of SeatOfPlanes. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBSeatOfPlanes.Update(
					this.id, 
					this.planeId, 
					this.seatClassId, 
					this.totalSeat, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of SeatOfPlanes. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.Id > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of SeatOfPlanes. Returns true on success.
		/// </summary>
		/// <param name="planeId"> planeId </param>
		/// <param name="seatClassId"> seatClassId </param>
		/// <returns>bool</returns>
		public static bool Delete(
			int Id) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBSeatOfPlanes.Delete(Id); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}
		
		
		/// <summary>
		/// Gets a count of SeatOfPlanes. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBSeatOfPlanes.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<SeatOfPlanes> LoadListFromReader(IDataReader reader)
		{
			List<SeatOfPlanes> seatOfPlanesList = new List<SeatOfPlanes>();
			try
			{
				while (reader.Read())
				{
					SeatOfPlanes seatOfPlanes = new SeatOfPlanes();
					seatOfPlanes.id = Convert.ToInt64(reader["Id"]);
					seatOfPlanes.planeId = Convert.ToInt64(reader["PlaneId"]);
					seatOfPlanes.seatClassId = Convert.ToInt32(reader["SeatClassId"]);
					seatOfPlanes.totalSeat = Convert.ToInt32(reader["TotalSeat"]);
					seatOfPlanes.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					seatOfPlanes.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					seatOfPlanesList.Add(seatOfPlanes);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return seatOfPlanesList;
		}
	
		/// <summary>
		/// Gets an IList with all instances of SeatOfPlanes.
		/// </summary>
		public static List<SeatOfPlanes> GetAll()
		{
			List<SeatOfPlanes> listSeatOfPlanes = new List<SeatOfPlanes>();
			try
			{
				IDataReader reader = DBSeatOfPlanes.GetAll();
				listSeatOfPlanes = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listSeatOfPlanes;
		}

        /// <summary>
        /// Gets an IList with all instances of SeatOfPlanes.
        /// </summary>
        public static List<SeatOfPlanes> GetAll(long planeId)
        {
            List<SeatOfPlanes> listSeatOfPlanes = new List<SeatOfPlanes>();
            try
            {
                IDataReader reader = DBSeatOfPlanes.GetAll(planeId);
                listSeatOfPlanes = LoadListFromReader(reader);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listSeatOfPlanes;
        }
	
		/// <summary>
		/// Gets an IList with page of instances of SeatOfPlanes.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<SeatOfPlanes> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<SeatOfPlanes> listSeatOfPlanes = new List<SeatOfPlanes>();
			try
			{
				IDataReader reader = DBSeatOfPlanes.GetPage(pageNumber, pageSize, out totalPages);	
				listSeatOfPlanes = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listSeatOfPlanes;
		}
		
		#endregion

	}
}





